﻿namespace Hidistro.SaleSystem.Shopping
{
    using Hidistro.Entities.Commodities;
    using Hidistro.Entities.Promotions;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.SqlDal.Commodities;
    using Hidistro.SqlDal.Promotions;
    using Hidistro.SqlDal.Sales;
    using System;
    using System.Collections.Generic;

    public static class ShoppingCartProcessor
    {
        public static bool AddGiftItem(int giftId, int quantity, PromoteType promotype)
        {
            if (HiContext.Current.User.IsAnonymous)
            {
                return new CookieShoppingDao().AddGiftItem(giftId, quantity);
            }
            return new ShoppingCartDao().AddGiftItem(giftId, quantity, promotype);
        }

        public static void AddLineItem(string skuId, int quantity)
        {
            Member user = HiContext.Current.User as Member;
            if (quantity <= 0)
            {
                quantity = 1;
            }
            if (user != null)
            {
                new ShoppingCartDao().AddLineItem(user, skuId, quantity);
            }
            else
            {
                new CookieShoppingDao().AddLineItem(skuId, quantity);
            }
        }

        public static void ClearCookieShoppingCart()
        {
            new CookieShoppingDao().ClearShoppingCart();
        }

        public static void ClearShoppingCart()
        {
            if (HiContext.Current.User.IsAnonymous)
            {
                new CookieShoppingDao().ClearShoppingCart();
            }
            else
            {
                new ShoppingCartDao().ClearShoppingCart(HiContext.Current.User.UserId);
            }
        }

        public static void ConvertShoppingCartToDataBase(ShoppingCartInfo shoppingCart)
        {
            Member user = HiContext.Current.User as Member;
            if (user != null)
            {
                ShoppingCartDao dao = new ShoppingCartDao();
                if (shoppingCart.LineItems.Count > 0)
                {
                    foreach (ShoppingCartItemInfo info in shoppingCart.LineItems)
                    {
                        dao.AddLineItem(user, info.SkuId, info.Quantity);
                    }
                }
                if (shoppingCart.LineGifts.Count > 0)
                {
                    foreach (ShoppingCartGiftInfo info2 in shoppingCart.LineGifts)
                    {
                        dao.AddGiftItem(info2.GiftId, info2.Quantity, (PromoteType)info2.PromoType);
                    }
                }
            }
        }

        public static ShoppingCartInfo GetCookieShoppingCart()
        {
            return new CookieShoppingDao().GetShoppingCart();
        }

        public static ShoppingCartInfo GetCountDownShoppingCart(string productSkuId, int buyAmount)
        {
            ShoppingCartInfo shoppingCartInfo = new ShoppingCartInfo();
            ShoppingCartItemInfo cartItemInfo = new ShoppingCartDao().GetCartItemInfo(HiContext.Current.User as Member, productSkuId, buyAmount);
            ShoppingCartInfo result;
            if (cartItemInfo == null)
            {
                result = null;
            }
            else
            {
                CountDownInfo countDownInfo = ProductBrowser.GetCountDownInfo(cartItemInfo.ProductId);
                if (countDownInfo == null)
                {
                    result = null;
                }
                else
                {
                    ShoppingCartItemInfo shoppingCartItemInfo = new ShoppingCartItemInfo();
                    shoppingCartItemInfo.SkuId = cartItemInfo.SkuId;
                    shoppingCartItemInfo.ProductId = cartItemInfo.ProductId;
                    shoppingCartItemInfo.SKU = cartItemInfo.SKU;
                    shoppingCartItemInfo.Name = cartItemInfo.Name;
                    shoppingCartItemInfo.MemberPrice = (shoppingCartItemInfo.AdjustedPrice = countDownInfo.CountDownPrice);
                    shoppingCartItemInfo.SkuContent = cartItemInfo.SkuContent;
                    ShoppingCartItemInfo arg_C7_0 = shoppingCartItemInfo;
                    shoppingCartItemInfo.ShippQuantity = buyAmount;
                    arg_C7_0.Quantity = buyAmount;
                    shoppingCartItemInfo.Weight = cartItemInfo.Weight;
                    shoppingCartItemInfo.ThumbnailUrl40 = cartItemInfo.ThumbnailUrl40;
                    shoppingCartItemInfo.ThumbnailUrl60 = cartItemInfo.ThumbnailUrl60;
                    shoppingCartItemInfo.ThumbnailUrl100 = cartItemInfo.ThumbnailUrl100;
                    shoppingCartItemInfo.IsfreeShipping = cartItemInfo.IsfreeShipping;
                    shoppingCartInfo.LineItems.Add(shoppingCartItemInfo);
                    result = shoppingCartInfo;
                    Member user = HiContext.Current.User as Member;
                    if (user == null) user = new Member(Hidistro.Membership.Core.Enums.UserRole.Member) { GradeId = 1 };
                    PromotionInfo info = new PromotionDao().GetSendPromotion(user, shoppingCartInfo.GetTotal(), PromoteType.FullAmountSentFreight);
                    if (info != null)
                    {
                        shoppingCartInfo.FreightFreePromotionId = info.ActivityId;
                        shoppingCartInfo.FreightFreePromotionName = info.Name;
                        shoppingCartInfo.IsFreightFree = true;
                    }
                }
            }
            return result;
        }

        public static int GetGiftItemQuantity(PromoteType promotype)
        {
            return new ShoppingCartDao().GetGiftItemQuantity(promotype);
        }

        public static ShoppingCartInfo GetGroupBuyShoppingCart(string productSkuId, int buyAmount)
        {
            ShoppingCartInfo shoppingCartInfo = new ShoppingCartInfo();
            ShoppingCartItemInfo cartItemInfo = new ShoppingCartDao().GetCartItemInfo(HiContext.Current.User as Member, productSkuId, buyAmount);
            ShoppingCartInfo result;
            if (cartItemInfo == null)
            {
                result = null;
            }
            else
            {
                GroupBuyInfo productGroupBuyInfo = ProductBrowser.GetProductGroupBuyInfo(cartItemInfo.ProductId);
                if (productGroupBuyInfo == null || productGroupBuyInfo.StartDate > DateTime.Now || productGroupBuyInfo.Status != GroupBuyStatus.UnderWay)
                {
                    result = null;
                }
                else
                {
                    ShoppingCartItemInfo shoppingCartItemInfo = new ShoppingCartItemInfo();
                    shoppingCartItemInfo.SkuId = cartItemInfo.SkuId;
                    shoppingCartItemInfo.ProductId = cartItemInfo.ProductId;
                    shoppingCartItemInfo.SKU = cartItemInfo.SKU;
                    shoppingCartItemInfo.Name = cartItemInfo.Name;
                    shoppingCartItemInfo.MemberPrice = (shoppingCartItemInfo.AdjustedPrice = productGroupBuyInfo.GroupBuyConditions[0].Price);
                    shoppingCartItemInfo.SkuContent = cartItemInfo.SkuContent;
                    ShoppingCartItemInfo arg_EC_0 = shoppingCartItemInfo;
                    shoppingCartItemInfo.ShippQuantity = buyAmount;
                    arg_EC_0.Quantity = buyAmount;
                    shoppingCartItemInfo.Weight = cartItemInfo.Weight;
                    shoppingCartItemInfo.ThumbnailUrl40 = cartItemInfo.ThumbnailUrl40;
                    shoppingCartItemInfo.ThumbnailUrl60 = cartItemInfo.ThumbnailUrl60;
                    shoppingCartItemInfo.ThumbnailUrl100 = cartItemInfo.ThumbnailUrl100;
                    shoppingCartItemInfo.IsfreeShipping = cartItemInfo.IsfreeShipping;
                    shoppingCartInfo.LineItems.Add(shoppingCartItemInfo);
                    result = shoppingCartInfo;
                    Member user = HiContext.Current.User as Member;
                    if (user == null) user = new Member(Hidistro.Membership.Core.Enums.UserRole.Member) { GradeId = 1 };
                    PromotionInfo info = new PromotionDao().GetSendPromotion(user, shoppingCartInfo.GetTotal(), PromoteType.FullAmountSentFreight);
                    if (info != null)
                    {
                        shoppingCartInfo.FreightFreePromotionId = info.ActivityId;
                        shoppingCartInfo.FreightFreePromotionName = info.Name;
                        shoppingCartInfo.IsFreightFree = true;
                    }
                }
            }
            return result;
        }

        public static ShoppingCartInfo GetShoppingCart(int Boundlingid, int buyAmount)
        {
            ShoppingCartInfo info = new ShoppingCartInfo();
            List<BundlingItemInfo> bundlingItemsByID = ProductBrowser.GetBundlingItemsByID(Boundlingid);
            Member user = HiContext.Current.User as Member;
            ShoppingCartDao dao = new ShoppingCartDao();
            foreach (BundlingItemInfo info2 in bundlingItemsByID)
            {
                ShoppingCartItemInfo item = dao.GetCartItemInfo(user, info2.SkuId, buyAmount * info2.ProductNum);
                if (item != null)
                {
                    info.LineItems.Add(item);
                }
            }
            if (user == null) user = new Member(Hidistro.Membership.Core.Enums.UserRole.Member) { GradeId = 1 };
            PromotionInfo info3 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentFreight);
            if (info3 != null)
            {
                info.FreightFreePromotionId = info3.ActivityId;
                info.FreightFreePromotionName = info3.Name;
                info.IsFreightFree = true;
            }
            return info;
        }
        public static ShoppingCartInfo GetShoppingCart(IList<string> NotSelect = null)
        {
            Member user = HiContext.Current.User as Member;
            if (user != null)
            {
                Juse.Interface.ShoppingCartInterface cart = new Juse.Interface.ShoppingCartInterface();
                JSShop.Model.ShoppingCartInfo result = cart.GetShoppingCartById(HiContext.Current.User.UserId);
                if (result == null || ((result.LineItems.Count == 0) && (result.LineGifts.Count == 0)))
                {
                    return null;
                }
                ShoppingCartInfo shoppingCart = new ShoppingCartInfo();
                foreach (var item in result.LineItems)
                {
                    shoppingCart.LineItems.Add(new ShoppingCartItemInfo()
                    {
                        AdjustedPrice = item.AdjustedPrice,
                        CategoryUrl = item.CategoryUrl,
                        IsfreeShipping = item.IsfreeShipping,
                        IsSelect = item.IsSelect,
                        IsSendGift = item.IsSendGift,
                        MemberPrice = item.MemberPrice,
                        Name = item.Name,
                        ProductId = item.ProductId,
                        PromoteType = (Hidistro.Entities.Promotions.PromoteType)(Enum.Parse(typeof(Hidistro.Entities.Promotions.PromoteType), item.PromoteType.ToString(), true)),
                        PromotionId = item.PromotionId,
                        PromotionName = item.PromotionName,
                        Quantity = item.Quantity,
                        ShippQuantity = item.ShippQuantity,
                        SKU = item.SKU,
                        SkuContent = item.SkuContent,
                        SkuId = item.SkuId,
                        ThumbnailUrl100 = item.ThumbnailUrl100,
                        ThumbnailUrl40 = item.ThumbnailUrl40,
                        ThumbnailUrl60 = item.ThumbnailUrl60,
                        UserId = item.UserId,
                        Weight = item.Weight
                    });
                }
                decimal reducedAmount = 0M;
                PromotionInfo[] info2 = new PromotionDao().GetReducedPromotions(user, shoppingCart.LineItems, out reducedAmount);
                if (info2 != null && info2.Length > 0)
                {
                    shoppingCart.ReducedPromotionInfo = info2;
                    shoppingCart.ReducedPromotionAmount = reducedAmount;
                    shoppingCart.IsReduced = true;
                }
                PromotionInfo info21 = new PromotionDao().GetReducedPromotion(user, shoppingCart.GetAmount(), shoppingCart.GetQuantity(), out reducedAmount);
                if (info21 != null)
                {
                    shoppingCart.ReducedPromotionId = info21.ActivityId;
                    shoppingCart.ReducedPromotionName = info21.Name;
                    shoppingCart.ReducedPromotionAmount += reducedAmount;
                    shoppingCart.IsReduced = true;
                }
                PromotionInfo[] info3 = new PromotionDao().GetSendPromotions(user, shoppingCart.LineItems, PromoteType.FullAmountSentGift);
                if (info3 != null && info3.Length > 0)
                {
                    shoppingCart.SendGiftPromotionInfo = info3;
                    shoppingCart.IsSendGift = true;
                }
                //PromotionInfo info3 = new PromotionDao().GetSendPromotion(user, shoppingCart.GetTotal(), PromoteType.FullAmountSentGift);
                //if (info3 != null)
                //{
                //    shoppingCart.SendGiftPromotionId = info3.ActivityId;
                //    shoppingCart.SendGiftPromotionName = info3.Name;
                //    shoppingCart.IsSendGift = true;
                //}
                PromotionInfo[] info4 = new PromotionDao().GetSendPromotions(user, shoppingCart.LineItems, PromoteType.FullAmountSentTimesPoint);
                if (info4 != null && info4.Length > 0)
                {
                    shoppingCart.SentTimesPointPromotionInfo = info4;
                    shoppingCart.IsSendTimesPoint = true;
                    decimal DiscountValue = 0;
                    for (int i = 0; i < info4.Length; i++)
                    {
                        DiscountValue += info4[i].DiscountValue;
                    }
                    shoppingCart.TimesPoint = DiscountValue;
                }
                PromotionInfo info41 = new PromotionDao().GetSendPromotion(user, shoppingCart.GetTotal(), PromoteType.FullAmountSentTimesPoint);
                if (info41 != null)
                {
                    shoppingCart.SentTimesPointPromotionId = info41.ActivityId;
                    shoppingCart.SentTimesPointPromotionName = info41.Name;
                    shoppingCart.IsSendTimesPoint = true;
                    shoppingCart.TimesPoint += info41.DiscountValue;
                }
                PromotionInfo info5 = new PromotionDao().GetSendPromotion(user, shoppingCart.GetTotal(), PromoteType.FullAmountSentFreight);
                if (info5 != null)
                {
                    shoppingCart.FreightFreePromotionId = info5.ActivityId;
                    shoppingCart.FreightFreePromotionName = info5.Name;
                    shoppingCart.IsFreightFree = true;
                }
                return shoppingCart;
            }
            return new CookieShoppingDao().GetShoppingCart(NotSelect);
        }

        public static ShoppingCartInfo GetShoppingCart_danping(string productSkuId, int buyAmount, bool spread = false)
        {
            ShoppingCartInfo info = new ShoppingCartInfo();
            Member user = HiContext.Current.User as Member;
            ShoppingCartItemInfo item = new ShoppingCartDao().GetCartItemInfo_danping(user, productSkuId, buyAmount);
            if (item == null)
            {
                return null;
            }
            info.LineItems.Add(item);
            if (user != null)
            {
                decimal reducedAmount = 0M;
                PromotionInfo[] info3 = new PromotionDao().GetReducedPromotions(user, info.LineItems, out reducedAmount);
                if (info3 != null && info3.Length > 0)
                {
                    info.ReducedPromotionInfo = info3;
                    info.ReducedPromotionAmount = reducedAmount;
                    info.IsReduced = true;
                }

                PromotionInfo[] info4 = new PromotionDao().GetSendPromotions(user, info.LineItems, PromoteType.FullAmountSentGift);
                if (info4 != null && info4.Length > 0)
                {
                    info.SendGiftPromotionInfo = info4;
                    info.IsSendGift = true;
                }

                PromotionInfo[] info5 = new PromotionDao().GetSendPromotions(user, info.LineItems, PromoteType.FullAmountSentTimesPoint);
                if (info5 != null && info5.Length > 0)
                {
                    info.SentTimesPointPromotionInfo = info5;
                    info.IsSendTimesPoint = true;
                    decimal DiscountValue = 0;
                    for (int i = 0; i < info5.Length; i++)
                    {
                        DiscountValue += info5[i].DiscountValue;
                    }
                    info.TimesPoint = DiscountValue;
                }

                PromotionInfo info6 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentFreight);
                if (info6 != null)
                {
                    info.FreightFreePromotionId = info6.ActivityId;
                    info.FreightFreePromotionName = info6.Name;
                    info.IsFreightFree = true;
                }
            }
            else if (spread)
            {
                user = new Member(Hidistro.Membership.Core.Enums.UserRole.Member) { GradeId = 1 };
                PromotionInfo info6 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentFreight);
                if (info6 != null)
                {
                    info.FreightFreePromotionId = info6.ActivityId;
                    info.FreightFreePromotionName = info6.Name;
                    info.IsFreightFree = true;
                }
            }
            return info;
        }


        public static ShoppingCartInfo GetShoppingCart(string productSkuId, int buyAmount, bool spread = false)
        {
            ShoppingCartInfo info = new ShoppingCartInfo();
            Member user = HiContext.Current.User as Member;
            ShoppingCartItemInfo item = new ShoppingCartDao().GetCartItemInfo(user, productSkuId, buyAmount);
            if (item == null)
            {
                return null;
            }
            info.LineItems.Add(item);
            if (user != null)
            {
                decimal reducedAmount = 0M;
                PromotionInfo[] info3 = new PromotionDao().GetReducedPromotions(user, info.LineItems, out reducedAmount);
                if (info3 != null && info3.Length > 0)
                {
                    info.ReducedPromotionInfo = info3;
                    info.ReducedPromotionAmount = reducedAmount;
                    info.IsReduced = true;
                }
                //PromotionInfo info3 = new PromotionDao().GetReducedPromotion(user, info.GetAmount(), info.GetQuantity(), out reducedAmount);
                //if (info3 != null)
                //{
                //    info.ReducedPromotionId = info3.ActivityId;
                //    info.ReducedPromotionName = info3.Name;
                //    info.ReducedPromotionAmount = reducedAmount;
                //    info.IsReduced = true;
                //}
                PromotionInfo[] info4 = new PromotionDao().GetSendPromotions(user, info.LineItems, PromoteType.FullAmountSentGift);
                if (info4 != null && info4.Length > 0)
                {
                    info.SendGiftPromotionInfo = info4;
                    info.IsSendGift = true;
                }
                //PromotionInfo info4 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentGift);
                //if (info4 != null)
                //{
                //    info.SendGiftPromotionId = info4.ActivityId;
                //    info.SendGiftPromotionName = info4.Name;
                //    info.IsSendGift = true;
                //}
                PromotionInfo[] info5 = new PromotionDao().GetSendPromotions(user, info.LineItems, PromoteType.FullAmountSentTimesPoint);
                if (info5 != null && info5.Length > 0)
                {
                    info.SentTimesPointPromotionInfo = info5;
                    info.IsSendTimesPoint = true;
                    decimal DiscountValue = 0;
                    for (int i = 0; i < info5.Length; i++)
                    {
                        DiscountValue += info5[i].DiscountValue;
                    }
                    info.TimesPoint = DiscountValue;
                }
                //PromotionInfo info5 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentTimesPoint);
                //if (info5 != null)
                //{
                //    info.SentTimesPointPromotionId = info5.ActivityId;
                //    info.SentTimesPointPromotionName = info5.Name;
                //    info.IsSendTimesPoint = true;
                //    info.TimesPoint = info5.DiscountValue;
                //}
                PromotionInfo info6 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentFreight);
                if (info6 != null)
                {
                    info.FreightFreePromotionId = info6.ActivityId;
                    info.FreightFreePromotionName = info6.Name;
                    info.IsFreightFree = true;
                }
            }
            else if (spread)
            {
                user = new Member(Hidistro.Membership.Core.Enums.UserRole.Member) { GradeId = 1 };
                PromotionInfo info6 = new PromotionDao().GetSendPromotion(user, info.GetTotal(), PromoteType.FullAmountSentFreight);
                if (info6 != null)
                {
                    info.FreightFreePromotionId = info6.ActivityId;
                    info.FreightFreePromotionName = info6.Name;
                    info.IsFreightFree = true;
                }
            }
            return info;
        }

        public static int GetSkuStock(string skuId)
        {
            SKUItem skuItem = new SkuDao().GetSkuItem(skuId);
            if (skuItem != null)
            {
                return skuItem.Stock;
            }
            return 0;
        }

        public static void RemoveGiftItem(int giftId, PromoteType promotype)
        {
            if (HiContext.Current.User.IsAnonymous)
            {
                new CookieShoppingDao().RemoveGiftItem(giftId);
            }
            else
            {
                new ShoppingCartDao().RemoveGiftItem(giftId, promotype);
            }
        }

        public static void RemoveLineItem(string skuId)
        {
            if (HiContext.Current.User.IsAnonymous)
            {
                new CookieShoppingDao().RemoveLineItem(skuId);
            }
            else
            {
                new ShoppingCartDao().RemoveLineItem(HiContext.Current.User.UserId, skuId);
            }
        }

        public static void UpdateGiftItemQuantity(int giftId, int quantity, PromoteType promotype)
        {
            Member user = HiContext.Current.User as Member;
            if (quantity <= 0)
            {
                RemoveGiftItem(giftId, promotype);
            }
            if (user == null)
            {
                new CookieShoppingDao().UpdateGiftItemQuantity(giftId, quantity);
            }
            else
            {
                new ShoppingCartDao().UpdateGiftItemQuantity(giftId, quantity, promotype);
            }
        }

        public static void UpdateLineItemQuantity(string skuId, int quantity)
        {
            Member user = HiContext.Current.User as Member;
            if (quantity <= 0)
            {
                RemoveLineItem(skuId);
            }
            if (user == null)
            {
                new CookieShoppingDao().UpdateLineItemQuantity(skuId, quantity);
            }
            else
            {
                new ShoppingCartDao().UpdateLineItemQuantity(user, skuId, quantity);
            }
        }
    }
}

